package cn.xianshu.user.service.impl;

import cn.xianshu.basic.exception.BasicException;
import cn.xianshu.basic.util.SmsContants;
import cn.xianshu.basic.util.SmsUtil;
import cn.xianshu.basic.util.StrUtils;
import cn.xianshu.basic.util.UserContants;
import cn.xianshu.user.domain.User;
import cn.xianshu.user.mapper.UserMapper;
import cn.xianshu.user.service.IVerifyCodeService;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private UserMapper userMapper; // 注入对象




    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;



    @Override
    public void sendSmsCode(Map<String, String> params) {
        String phone = params.get("phone");
        String type = params.get("type");


        // 1.校验
        // 1.2验证手机号不能为空
        if (!StringUtils.hasLength(phone) || !StringUtils.hasLength(type)) {
            throw new BuilderException("手机不能为空!");
        }

        String redisKey =null;
        if (type.equals("register")){ // 注册流程
            // 手机号是否存在 t_user
            User user = userMapper.loadByPhone(phone);
            if (user != null){
                throw new BuilderException("手机号已注册!");
            }
            redisKey = UserContants.BINDER_PREFIX + phone;
            sendCode(redisKey);
        } else if (type.equals("binder")){
            redisKey = UserContants.BINDER_PREFIX + phone;
            sendCode(redisKey);
        }



    }

    /**
     * 发送验证码校验
     * @param redisKey
     */
    public void sendCode(String redisKey){
        // 3. 三分中过期， 从redis中取，如果能取到，没有过期
        // key: 业务标识+手机号    register_prefix+phone
        Object tmpCode = redisTemplate.opsForValue().get(redisKey);

        String code = null;
        if(tmpCode != null){
            // 3.1. 判断当前验证码是否超过一分钟，没有超过一分钟，直接提示：验证码已发送，请勿。。。
            // 获取时间戳
            String time = tmpCode.toString().split(":")[0];
            if(System.currentTimeMillis()-Long.valueOf(time)<= 1*60*1000){    // value: 时间戳+":"+code      65468731654654+":"+code
                throw new BasicException("验证码已发送，请勿重新发送。。。");
            }else{
                // 3.2. 超过一分钟，重新刷新之前验证码的过期时间
                code = tmpCode.toString().split(":")[1];
            }
        }else{
            // 4. 如果取不到， 从新生成验证码，
            code = StrUtils.getComplexRandomString(4);
        }
        redisTemplate.opsForValue().set(redisKey,
                System.currentTimeMillis()+":"+code,
                3L,
                TimeUnit.MINUTES);

        // SendMsg.sendSMS(phone, "验证码:"+code+",请三分中内使用");
        System.out.println(code);
    }
}

